import validate from 'wechat-validate'

Page({
  data: {
    currentDate: new Date().getTime(),
    houseLayerVisible: false,
    repairLayerVisible: false,
    dateLayerVisible: false,
    houseList: [],
    houseInfo: '',
    repairItemName: '',
    repairItem: [],
    attachment: [],
    id: undefined,
    houseId: '',
    repairItemId: '',
    mobile: '',
    appointment: '', //预约时间
    description: '', //报修描述
  },
  behaviors: [validate],
  rules: {
    mobile: [
      { required: true, message: '请填写手机号码!' },
      { pattern: /^1[3-8]\d{9}$/, message: '请检查手机号码是否正确!' },
    ],
    houseId: [{ required: true, message: '请选择报修房屋!' }],
    repairItemId: [{ required: true, message: '请选择报修项目!' }],
    appointment: [{ required: true, message: '请选择预约时间!' }],
    description: [{ required: true, message: '请填写报修描述!' }],
    attachment: [{ required: true, message: '请上传附件!' }],
  },
  openHouseLayer() {
    this.setData({ houseLayerVisible: true })
  },
  closeHouseLayer() {
    this.setData({ houseLayerVisible: false })
  },
  openRepairLayer() {
    this.setData({ repairLayerVisible: true })
  },
  closeRepairLayer() {
    this.setData({
      repairLayerVisible: false,
    })
  },

  openDateLayer() {
    this.setData({ dateLayerVisible: true })
  },
  closeDateLayer() {
    this.setData({ dateLayerVisible: false })
  },
  goList() {
    wx.reLaunch({
      url: '/repair_pkg/pages/list/index',
    })
  },
  onLoad({ id }) {
    this.getHouseList()
    this.getRepairItem()
    if (id) {
      this.getDetial(id)
      wx.setNavigationBarTitle({ title: '修改报修信息' })
    } else {
      wx.setNavigationBarTitle({ title: '填写报修信息' })
    }
  },
  // 获取报修详情
  async getDetial(id) {
    const res = await wx.http.get(`/repair/${id}`)
    this.setData({ ...res.data })
  },
  async getHouseList() {
    const res = await wx.http.get('/house')
    this.setData({
      houseList: res.data,
    })
  },
  async getRepairItem() {
    const res = await wx.http.get('/repairItem')
    this.setData({
      repairItem: res.data,
    })
  },
  // 选择报修房屋
  selectHouse(e) {
    this.setData({
      houseInfo: e.detail.name,
      houseId: e.detail.id,
    })
  },
  // 选择报修项目
  selectRepair(e) {
    this.setData({
      repairItemName: e.detail.name,
      repairItemId: e.detail.id,
    })
  },
  // 选择预约时间
  selectDate(e) {
    const date = new Date(e.detail)
    this.setData({
      appointment: date.toLocaleDateString().replaceAll('/', '-'),
      dateLayerVisible: false,
    })
  },
  beforeRead(e) {
    const { file, callback } = e.detail
    if (file.size > 1024 * 1024 * 2) {
      wx.utils.toast('文件大小不能超过2M')
      return callback(false)
    }
    callback(true)
  },
  async afterRead(e) {
    const res = await wx.http.upload('/upload', {
      name: 'file',
      filePath: e.detail.file.url,
    })
    this.setData({
      attachment: [...this.data.attachment, res.data],
    })
  },
  // 删除附件图片
  deleteAttachment(e) {
    this.setData({
      attachment: this.data.attachment.filter((item, index) => index !== e.detail.index),
    })
  },
  // 提交保修信息
  async onSubmit() {
    if (!this.validate()) return
    const { id, houseId, repairItemId, mobile, appointment, description, attachment } = this.data
    const { code } = await wx.http.post('/repair', {
      id,
      houseId,
      repairItemId,
      mobile,
      appointment,
      description,
      attachment,
    })
    if (code !== 10000) return wx.utils.toast('在线报修失败!')
    wx.navigateTo({ url: '/repair_pkg/pages/list/index' })
  },
})
